<script language='JavaScript1.2'>
	function updateRequestStatus(selectElement, requestId, noticeTarget)
	{
		var path = './json/request/status/' + requestId;
		var successHtml = '<img style="vertical-align:middle;" src="public/images/icons/check.png" alt="status update succeeded">';
		var failHtml = '<img style="vertical-align:middle;" src="public/images/icons/cross.png" alt="status update failed">';
		var waitHtml = '<img style="vertical-align:middle;" src="public/images/icons/ajax-loader.gif" alt="status update pending">';
		$(noticeTarget).html(waitHtml);
		$.ajax({
			url: path,
			type: 'POST',
			data: {'status' : $(selectElement).val()},
			dataType: 'json',
			success: function(data){ 
				if (data && data.success ) {
					if ( 
						data.request 
						&& data.request[0]
						&& data.request[0].status == $(selectElement).val()
					) {
						$(noticeTarget).html(successHtml);
						updateInterfaceStatus(selectElement);
					} else {
						$(noticeTarget).html(failHtml);
						alert('Unexpected error, status not set correctly.');
						revertInterfaceStatus(selectElement);
					}
				} else {
					$(noticeTarget).html(failHtml);
					if (data && data.message) {
						alert(data.message);
					} else {
						alert('Failed to update this request\'s status.');
					}
					revertInterfaceStatus(selectElement);
				}				
			},
			error : function(junk, junk2, junk3){
				$(noticeTarget).html(failHtml);
				alert('failed to update this request\'s status.');
				revertInterfaceStatus(selectElement);
			}
		});	
		return false;
	}

	function revertInterfaceStatus(selectElement)
	{
		var lastStoredValue = $(selectElement).data('lastStoredValue');
		$(selectElement).val(lastStoredValue);
		var optionElement = $('option[value="' + lastStoredValue +'"]', selectElement);
		var buttonElement = $(selectElement).parent('.ui-btn').find('.ui-btn-text');
		buttonElement.html(optionElement.html());
	}

	function updateInterfaceStatus(selectElement)
	{
		var lastStoredValue = $(selectElement).val();
		$(selectElement).data('lastStoredValue', lastStoredValue);
	}

	$(document).ready(function() {  //#TODO switch to .on('ready
		$('[name="status"]').each(function() {
			updateInterfaceStatus(this);
		});
	});
	
</script>
<?php 
	$filterLibrary = 
		is_numeric($this->libraryId) && 0 != $this->libraryId
		? new library($this->libraryId)
		: NULL;
	$filterLibraryLabel = (
		$filterLibrary
		? $filterLibrary->getLibraryName()
		: 'All Libraries'
	);
	$currentStatus = 
		is_numeric($this->statusId) && 0 != $this->statusId
		? new Rd_Request_Status($this->statusId)
		: NULL;
	$filterStatusLabel = (
		$currentStatus
		? $currentStatus->getLabel()
		: 'All Statuses'
	);

?>
<div class="preloadIcons">
	<img style="vertical-align:middle;" src="public/images/icons/check.png" alt="">
	<img style="vertical-align:middle;" src="public/images/icons/cross.png" alt="">
	<img style="vertical-align:middle;" src="public/images/icons/ajax-loader.gif" alt="">
</div>
<?php 
$alert = trim(Rd_Layout::getMessage('generalAlert'));
if ('' != $alert) { ?>
<p class="focal alert">Unable to sort items by Call Number at this time. Please try again.</p>
<?php } ?>
<div class="focal"><p class="notruncate">Current Requests for: <?php print($filterLibraryLabel); ?>, <?php print($filterStatusLabel); ?></p></div>
<!-- <ul data-role="listview" class="results listWithSubtext noBottomMargin" data-inset="false">
<li data-role="list-divider" class="ui-bar-a ui-li ui-li-divider ui-btn ui-bar-b ui-btn-up-undefined" role="heading">Request Filter Options</li>
</ul> -->
<div data-role="collapsible">
<h3>Filter And Sorting Options</h3>
<form method="get" action="./">
	<input type="hidden" name="cmd" value="displayRequest"/>
	<div data-role="fieldcontain">
		<label>
			<span class="mobileBlockLabel">Status</span>
			<!--<select name="whatever2"><option value="test1">Search Stacks</option><option value="test2">test2</option></select> -->
			<?php $this->getDisplayer()->renderReserveStatusSelect(array(
				'defaultStatus' => $this->statusId,
				'includeAllOption' => TRUE,
				'excludeActive' => TRUE,
				'excludeInactive' => TRUE,
				'excludeDenied' => TRUE,
				'fieldName' => 'statusId'
			));?>

		</label>
	</div>
	<div data-role="fieldcontain">
		<label>
			<span class="mobileBlockLabel">Library</span>
			<!--<select name="whatever"><option value="test1">D.H. Hill</option><option value="test2">test2</option></select> -->
			<?php $this->getDisplayer()->renderLibrarySelect(array(
				'defaultLibrary' => ($filterLibrary ? $filterLibrary->getId() : '*'),
				'includeAllOption' => TRUE
			));?>
		</label>
	</div>
	<div data-role="fieldcontain">
		<label>
			<span class="mobileBlockLabel">Type</span>
			<select name="type"><?php 
				$typeOptions = array('BOTH' => 'Both', 'PHYSICAL' => 'Physical', 'SCAN' => 'Electronic');
				foreach($typeOptions as $fieldValue=>$fieldLabel){
					$attributes = (
						$this->filterType === $fieldValue
						? ' selected="selected"'
						: ''
					);
			?>
			<option value="<?php print($fieldValue); ?>" <?php print($attributes);?>><?php print($fieldLabel);?></option>
			<?php 
				}
			?></select>
		</label>
	</div>
	<div data-role="fieldcontain">
		<label>
			<span class="mobileBlockLabel">Sort</span>
			<select name="sort"><?php 
				$typeOptions = array('callnum' => 'Call Number', 'needed' => 'Needed By Date');
				foreach($typeOptions as $fieldValue=>$fieldLabel){
					$attributes = (
						$this->sort === $fieldValue
						? ' selected="selected"'
						: ''
					);
			?>
			<option value="<?php print($fieldValue); ?>" <?php print($attributes);?>><?php print($fieldLabel);?></option>
			<?php 
				}
			?></select>
		</label>
	</div>
	<div data-role="fieldcontain">
		<label>
			<span class="mobileBlockLabel">Direction</span>
			<select name="dir">
				<option value="asc" <?php print($this->dir == 'asc' ? 'selected="selected"' : '');?>>Ascending</option>
				<option value="desc" <?php print($this->dir == 'desc' ? 'selected="selected"' : '');?>>Descending</option>
			</select>
		</label>
	</div>
	<input type="submit" name="submit" value="Update"/>
</form>
</div>
<ul data-role="listview" class="doubleTopMargin doubleBottomMargin listWithSubtext" data-inset="false">
	<li data-role="list-divider" class="ui-bar-a ui-li ui-li-divider ui-btn ui-bar-b ui-btn-up-undefined" role="heading">
		Matching Requests (<?php print(count($this->requestList));?>)
		<span id="statusUpdateError"></span>
	</li>
<?php foreach($this->requestList as $request) { 
			$requestId = $request->getId();
			$item = $request->requestedItem;
			//$ci = $request->courseInstance;
			$callNumbers = $item->getAllCallNumbers();
			$callNumberCount = count($callNumbers);
			$firstCallNumber = array_shift($callNumbers);
			foreach($callNumbers as $callNumber) {
				$firstCallNumber = $callNumber;
				break;
			}
			//$physicalCopy = $item->physicalCopy;
			//if ($physicalCopy) {
			//	$callNumbers[] = $physicalCopy->getCallNumber();
			//}
			$requestNotes = $request->getAllNotes();	
	?>
	<li class="<?php print('' != trim($item->getControlNumber()) ? 'sortable' :'notSortable');?>">
	<?php 
		$volumeTitle = trim($item->getVolumeTitle());
		$volumeEdition = trim($item->getVolumeEdition());
	?>		
		<p class="alignRight"><?php print($request->getType()); ?></p>
	<?php if ('SCAN' == strtoupper($request->getType()) && '' != trim($volumeTitle)) { ?>
		<p>Volume Title: <?php print($volumeTitle); ?></p>
	<?php } ?>
		<h3><?php print($item->getTitle());?></h3>
	<?php if ('' != trim($item->getAuthor())) { ?>
		<p>Author: <?php print($item->getAuthor());?></p>
	<?php } ?>
	<?php if ('SCAN' != strtoupper($request->getType())) {
		if ('' != ($volumeTitle . $volumeEdition)) { ?>
		<p>Volume: <?php print(
			('' != $volumeTitle ? $volumeTitle . ' (Title) ' : '')
			. ('' != $volumeEdition ? $volumeEdition . ' (Edition) ' : '')
		);?></p>
	<?php }
		} else {
			if ('' != trim($volumeEdition)) {
		?>
		<p>Volume Edition: <?php print($volumeEdition); ?></p>
		<?php
			}
		
		} ?>
		<div <?php print($callNumberCount > 1 ? 'data-role="collapsible"' : 'class="marginCenter"'); ?>><h3>Call Number: <?php 
			print(
				$callNumberCount > 0 && '' != trim($firstCallNumber)
				? $firstCallNumber
				: ( 
					'' != trim($item->getOldId()) 
					? ($item->getOldId() . " <b>(Tentative)</b>") 
					: '' 
				)	
			);
			print($callNumberCount > 1 ? (' (+ ' . ($callNumberCount - 1) .')') : '');
			?></h3>
			<?php if ($callNumberCount > 1) {?>
				<ul class="marginCenter noBullet">
				<?php foreach($callNumbers as $callNumberIndex => $callNumber) { ?>
					<li><?php print($callNumber); ?></li>
				<?php } ?>
				</ul>
			<?php } ?>
		</div>
		<?php if (count($requestNotes) > 0) { ?>
		<div data-role="collapsible">
			<h3>Notes (<?php print(count($requestNotes)); ?>)</h3>
			<?php print($this->getDisplayer()->renderNotes($requestNotes));?>
		</div>
		<?php } ?>
		<form action="#" method="get">
			<input type="hidden" name="requestId" value="<?php print($requestId);?>"/>
			<div data-role="fieldcontain">
			<label>
				<span class="mobileBlockLabel">Status</span>
				<?php 
				$this->getDisplayer()->renderReserveStatusSelect(array(
				'defaultStatus' => $request->getStatus(),
				'showDefault' => TRUE,
				'excludeActive' => TRUE,
				'excludeInactive' => TRUE,
				'excludeDenied' => TRUE,
				'fieldName' => 'status',
				'onChange' => "updateRequestStatus(this, {$requestId} , '#noticeFor_{$requestId}');"
			));?><div class="inlineIcon" id="noticeFor_<?php print($requestId);?>"></div>
			</label>
		</div>
		</form>
		
	</li>
<?php } 
	if (count($this->requestList) == 0) {
?>		
	<li>No requests matched this search.</li>
<?php 
		}
?>
</ul>
<?php
